package com.explorati.design_pattern.hybrid.specification;

//like规格书
public class UserByNameLike extends CompositeSpecification {

	// like的标记
	private final String LIKE = "%";
	// 基准like字符串
	private String likeStr;

	public UserByNameLike(String name) {
		this.likeStr = name;
	}

	// 实现检验方法
	@Override
	public boolean isSatisfiedBy(User user) {
		boolean result = false;
		String name = user.getName();
		// 去掉%后的字符串
		String str = likeStr.replace("%", "").trim();
		if (likeStr.startsWith(LIKE) && likeStr.endsWith(LIKE)) {
			// 类似于"%张%"
			result = name.contains(str);
		} else if (likeStr.startsWith(LIKE)) {
			// 类似于"%张"
			result = name.endsWith(str);
		} else {
			// 类似于"张%"
			result = name.startsWith(str);
		}
		return result;
	}

}
